package ltd.jdsoft.cute.channel.selector;

import java.util.ArrayList;
import java.util.List;

import ltd.jdsoft.cute.channel.model.Channel;

/**
 * 通道权重的计算处理
 * 
 * @author zhengzhq
 *
 */
public class ChannelWeightProcessor {

  /**
   * 根据提供的通道列表计算对应的权重比例
   * 
   * @param channelList
   * @return
   */
  public static List<ChannelWeight> getChannelWeightList(List<Channel> channelList) {
    List<ChannelWeight> channelWeightList = new ArrayList<ChannelWeight>();
    // 计算所有权重的综合
    int totalWeight = 0;
    for (Channel channel : channelList) {
      totalWeight += channel.getWeight();
    }

    // 将综合当作“1”，计算每个通道在1中占的比例，该值也是每个通道的最小值
    double number = 0;
    for (Channel channel : channelList) {
      number += 1.0 * channel.getWeight() / totalWeight;
      channelWeightList.add(new ChannelWeight(number, channel));
    }

    return channelWeightList;
  }

  static class ChannelWeight {
    // 当前通道占用的比例
    private double rate;
    // 当前通道对象
    private Channel channel;

    public ChannelWeight(double rate, Channel channel) {
      this.rate = rate;
      this.channel = channel;
    }

    public double getRate() {
      return rate;
    }

    public void setRate(double rate) {
      this.rate = rate;
    }

    public Channel getChannel() {
      return channel;
    }

    public void setChannel(Channel channel) {
      this.channel = channel;
    }
  }
}
